﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Text.RegularExpressions;


namespace RegExpressModel
{
	public abstract class BaseRegexModel
	{
		private string _regExpression;
		private bool _ignoreCase;
		private bool _ignorePatternWhitespace;
		private bool _dotMatchesNewline;
		private bool _isMultiline;



		protected BaseRegexModel()
        {
            IsValid = false;
        }


		public string RegExpression
		{
			get { return _regExpression; }
			set
			{
				if(_regExpression == value)
					return;

				_regExpression = value;
				RefreshRegEx();
			}
		}


		public bool IsCaseIgnored
		{
			get { return _ignoreCase; }
			set
			{
				if(_ignoreCase == value)
					return;

				_ignoreCase = value;
				RefreshRegEx();
			}
		}


		public bool IsMultiLine
		{
			get { return _isMultiline; }
			set
			{
				if(_isMultiline == value)
					return;

				_isMultiline = value;
				RefreshRegEx();
			}
		}


		public bool IsWhitespaceIgnored
		{
			get { return _ignorePatternWhitespace; }
			set
			{
				if(_ignorePatternWhitespace == value)
					return;

				_ignorePatternWhitespace = value;
				RefreshRegEx();
			}
		}


		public bool IsNewlineMatchedByDot
		{
			get { return _dotMatchesNewline; }
			set
			{
				if(_dotMatchesNewline == value)
					return;

				_dotMatchesNewline = value;
				RefreshRegEx();
			}
		}


		public bool IsValid { get; protected set; }


		protected Regex RegexObject { get; private set; }



		protected void RefreshRegEx()
		{
			ResetResults();
			try
			{
				RegexObject = new Regex(_regExpression, AggregateOptions());
				IsValid = true;
			}
			catch(Exception)
			{
				RegexObject = null;
				IsValid = false;
			}
		}



		/// <summary>
		/// Resets the results when either the.
		/// </summary>
		protected virtual void ResetResults()
		{
			//Derived classes should override this
		}



		protected RegexOptions AggregateOptions()
		{
			RegexOptions options = RegexOptions.Compiled | RegexOptions.CultureInvariant;

			if(_ignoreCase)
				options |= RegexOptions.IgnoreCase;
			if(_ignorePatternWhitespace)
				options |= RegexOptions.IgnorePatternWhitespace;
			if(_dotMatchesNewline)
				options |= RegexOptions.Singleline;
			if(_isMultiline)
				options |= RegexOptions.Multiline;

			return options;
		}

	
	}
}
